package com.itcanteen.medical.patient.controller;

import com.itcanteen.medical.common.constant.Constant;
import com.itcanteen.medical.common.exception.AdException;
import com.itcanteen.medical.common.vo.CommonResponse;
import com.itcanteen.medical.patient.entity.Information;
import com.itcanteen.medical.patient.service.InformationService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 新闻 控制层
 * @author 1804A-TeamOne
 * @date 2020-12-17 16:08
 */
@RestController
@RequestMapping("/information")
@CrossOrigin
public class InformationController {

    @Resource
    private InformationService informationService;

    @Resource
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 新闻列表  只展示序号 和 标题
     * @author 1804A-TeamOne
     * @date 2020-12-17 14:45
     * @return java.util.List<java.lang.Object>
     */
    @GetMapping("/selectAll")
    public List<Object> selectAll(){
        List<Information> informationList = informationService.selectAll();
        List< Object> list = new ArrayList<>();
        for (Information information : informationList) {
            list.add(information.getId());
            list.add(information.getTitle());
        }
        return list;
    }

    /**
     * 点击查看新闻按钮时  传进来新闻的ID  返回当前新闻的内容
     * @author 1804A-TeamOne
     * @date 2020-12-17 14:48
     * @param id id
     * @return CommonResponse
     */
    @GetMapping("/selectOne")
    public CommonResponse<Object> selectOne(Integer id) throws AdException {
        if(id == null){
            throw new AdException(Constant.ErrorMsg.REQUEST_PARAM_ERROR);
        }
        Information information = informationService.selectOne(id);
        Integer sum = (Integer)redisTemplate.boundHashOps("information").get("information"+id);
        if(sum == null){
            redisTemplate.boundHashOps("information").put("information"+id,1);
        }else{
            redisTemplate.boundHashOps("information").put("information"+id,sum+1);
        }
        return new CommonResponse<>(0,"success", information.getTitle() +"\n"+ information.getCenter());

    }

    /**
     * 点击热点咨询按钮 查看当前热门新闻 前5条
     * @author 1804A-TeamOne
     * @date 2020-12-17 14:48
     * @return java.util.List<java.lang.Object>
     */
    @GetMapping("/Consultant")
    public List<Object> consultant (){
        List<Information> informationList = informationService.orderBy();
        List< Object> list = new ArrayList<>();
        for (Information information : informationList) {
            list.add(information.getTitle());
            list.add(information.getCenter());
            list.add(information.getClicker());
        }
        return list;
    }

}
